cc.Class({
    extends: require('UI-PanelWindow'),

    properties: {
        guang:[cc.Node]
    },
    onLoad(){
        this._super();

        for(let i=0;i<this.guang.length;i++){
            this.guang[i].runAction(cc.repeatForever(cc.sequence(cc.fadeTo(0.3,120),cc.fadeTo(0.3,255))));
            this.guang[i].active=false;
        }

        for(let i=0;i<global.Data.prop.length;i++){
            if(global.Data.prop[i]){
                this.changeBg(i);
            }
        }
        this.blink(null);
    },
    
    click(e,d){
        var videoId='';
        var item=null;
        if(d=='baozou'){
            videoId='adunit-6ae6fefabc5f98d8';
            item=0
        }
        if(d=='weili'){
            videoId='adunit-e63d5f9f85652ec1';
            item=1
        }
        if(d=='hudun'){
            videoId='adunit-8894c62c6aabf953';
            item=2
        }
        this.onVideoget(videoId,item)
    },

    setOptions(options){
        this.hasWatchVideoJoin=options;
    },

    onVideoget(id,item){
        var self=this;
        var options={
            adUnitId:id,
            stopCb:function(){
                global.EJMgr.showToast('视频未看完，不能获得奖励')
            },
            successCb:function(){
                self.changeBg(item);
                self.blink(item);
            }
        }
        global.WechatShare.videoOrShare(options)
    },

    changeBg(item){
        var prop=cc.find('view/propBtn',this.node).children[item];
        prop.color = new cc.Color(36,226,135);
        prop.getComponent(cc.Button).interactable=false;
        cc.find('video/kanshiping',prop).active=false;
        cc.find('video/tips',prop).getComponent(cc.Label).string='已携带';
        global.Data.prop[item]=true;
    },

    blink(item){
        if(this.random!=item && item!=null){
            this.guang[this.random].active=true;
            return
        }
        for(let i=0;i<this.guang.length;i++){
            this.guang[i].active=false;
        }
        var arr=[]
        for(let i=0;i<global.Data.prop.length;i++){
            if(!global.Data.prop[i]){
                arr.push(i)
            }
        }
        this.random=arr[Math.floor(Math.random()*arr.length)]
        if(arr.length>0){
            this.guang[this.random].active=true;
        }
        
    },

    startGame(){
        global.firstResult=0;
        function joinGame(){
            SoundMgr.playSound('Audio/DoorClose')
            var door = cc.find('Canvas/door');
            door.active = true;
            door.js.moveIn(() => {
                global.Loader.loadResources('GameLoop', function (err, res) {
                    global.UIMgr.clearNode();
                    global.GameLoop.enterGameScene();
                    var audio='wujin';
                    if(global.gates!=0){
                        audio=global.gates.split('-')[0]
                    }
                    
                    SoundMgr.playMusic('./Audio/bgm_' + audio)
                    global.EJMgr.popUI();
                })
            });
        }
        if(!this.hasWatchVideoJoin){
            global.isTestPlay = false;
            // this.httpGet('/game/gameUser','attack',{},function(err,res){
            //     if(!err){
                    joinGame();
            //     }
            // })
        } else {
            global.isTestPlay = true;
            joinGame();
        }
        // global.EJMgr.popUI();
    }, 

    close(){
        global.EJMgr.popUI();
    },
});
